#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.infrastructure.persistent.repository.user;



import ${package}.domain.user.model.entity.UserEntity;
import ${package}.domain.user.model.valobj.GenderEnum;
import ${package}.domain.user.repository.IUserRepository;
import ${package}.infrastructure.persistent.dao.IUserDao;
import ${package}.infrastructure.persistent.po.UserPO;
import ${package}.infrastructure.persistent.repository.user.mapstruct.UserPOConvert;
import ${package}.types.exception.AppException;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;


import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Repository("userRepository")
public class UserRepository implements IUserRepository {
    @Resource
    private IUserDao userDao;

    @Override
    public void delete(List<Long> userIds) {
        if (CollectionUtils.isEmpty(userIds)) {
            throw new AppException("参数缺失");
        }
        userDao.delete(userIds);
    }

    @Override
    public void save(UserEntity userEntity) {
        if(Objects.isNull(userEntity)){
            throw new AppException("参数错误");
        }
        // 如果觉得mapstruct不好用，也可以自己写转换方法，但是不要使用BeanUtils.copyProperties();因为1.性能不好，2.随着项目的迭代转换不可控
        UserPO userPO = UserPOConvert.INSTANCE.convertUserEntityToUserPO(userEntity);
        userDao.insert(userPO);
    }

    @Override
    public UserEntity findByUserId(Long userId) {
        if (userId == null) {
            throw new AppException("参数缺失");
        }
        UserPO userPO = userDao.selectByUserId(userId);
        // 此时数字就转化为枚举值了
        return UserPOConvert.INSTANCE.convertUserPOToUserEntity(userPO);
    }

    @Override
    public void update(UserEntity userEntity) {
        if(Objects.isNull(userEntity) || Objects.isNull(userEntity.getId())){
            throw new AppException("参数错误");
        }
        // 不使用mapstruct，可以使用set方法
        UserPO userPO = new UserPO();
        userPO.setId(userEntity.getId());
        userPO.setName(userEntity.getName());
        userPO.setPassword(userEntity.getPassword());
        userPO.setGender(GenderEnum.getCode(userEntity.getGender()));
        userPO.setUpdateTime(LocalDateTime.now());
        userDao.update(userPO);
    }
}
